﻿using System;
using System.Collections.Generic;
using System.Text;
using DDDProject.Aspects;
using DDDProjectSample.Domain.Repositories;

namespace DDDProjectSample.Domain
{
    public class Topic : Entity
    {
        private string _title;
        private Forum _forum;
        private DateTime _creationDate;
        private Member _author;

        [InjectComponent]
        private IMessageRepository _messageRepository;

        /// <summary>
        /// Creates a new instance of class Topic
        /// </summary>
        protected Topic()
        {
        }

        /// <summary>
        /// Creates a new instance of class Topic
        /// </summary>
        /// <param name="title">Title of the topic</param>
        /// <param name="forum">Forum that the topic belongs to</param>
        /// <param name="author">User who creates the topic</param>
        public Topic(
            String title,
            Forum forum,
            Member author)
        {
            _title = title;
            _creationDate = DateTime.Now;
            _forum = forum;
            _author = author;
        }

        /// <summary>
        /// Gets the topic's title
        /// </summary>
        public virtual String Title
        {
            get { return _title; }
        }

        /// <summary>
        /// Gets the forum that the topic belongs to
        /// </summary>
        public virtual Forum Forum
        {
            get { return _forum; }
        }

        /// <summary>
        /// Gets the topic's creation date
        /// </summary>
        public virtual DateTime CreationDate
        {
            get { return _creationDate; }
        }

        /// <summary>
        /// Gets the user who creates the topic
        /// </summary>
        public virtual Member Author
        {
            get { return _author; }
        }

        /// <summary>
        /// Adds a new message to the topic
        /// </summary>
        /// <param name="text">Text of the message</param>
        /// <param name="author">Author of the message</param>
        /// <returns>The new created message</returns>
        public virtual Message AddMessage(
            String text,
            Member author)
        {
            // Creates the message
            Message message = new Message(text, author, this);

            // Saves the message
            _messageRepository.Save(message);

            // Returns the message
            return message;
        }
    }
}
